from unittest import TestCase
from unittest.mock import MagicMock, patch

from django_sso.business import (
    _get_user,
    _login_user,
    _logout_user,
    _redirect_url,
    _service_url,
)


class DjangoAuthBusinessTests(TestCase):
    def test_get_user(self):
        request = MagicMock()
        ticket = "mock_ticket"
        service = "foo.bar"
        with (patch("django_sso.business.authenticate")):
            _ = _get_user(request, service, ticket)

    def test_login_user(self):
        request = MagicMock()
        user = MagicMock()
        with (patch("django_sso.business.django_login")):
            _ = _login_user(request, user)

    def test_logout_user(self):
        request = MagicMock()
        with (patch("django_sso.business.django_logout")):
            _ = _logout_user(request)


class PvtBusinessTests(TestCase):
    def test_service_url(self):
        request = MagicMock()
        request.is_secure.return_value = True
        request.get_host.return_value = "foo.example.com"
        request.path = "/?foo"
        redirect_to = "example.com"
        with (patch("django_sso.business.authenticate")):
            _ = _service_url(request, redirect_to)

    def test_redirect_url(self):
        request = MagicMock()
        request.GET = {}
        settings = MagicMock()
        settings.CAS_IGNORE_REFERER = False
        with (patch("django_sso.business.authenticate")):
            _ = _redirect_url(request, settings)
